/**
 * created by 小卷毛, 2019/08/30 0021
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.af.community.smart.base

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.IdRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.jakewharton.rxbinding3.view.clicks
import org.jetbrains.anko.design.listeners.__TabLayout_OnTabSelectedListener
import java.util.concurrent.TimeUnit

inline fun <reified T : View> T.visible() { visibility = View.VISIBLE }

inline fun <reified T : View> T.invisible() { visibility = View.INVISIBLE }

inline fun <reified T : View> T.gone() { visibility = View.GONE }

inline fun <reified T : View> T.isVisble() = visibility == View.VISIBLE

inline fun <reified T : View> View.find(@IdRes id: Int): T = findViewById(id)

inline fun <reified T : TextView> T.clear() { text = "" }

fun Context.getColorEx(@ColorRes id: Int) = ContextCompat.getColor(this, id)

/**
 * 跳转到拨号页面
 */
fun Context.makeCall(number: String) =
    try {
        startActivity(
            Intent(
                Intent.ACTION_DIAL,
                Uri.parse("tel:$number")
            ).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
        )
        true
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }

/**
 * 跳转到拨号页面
 */
fun Fragment.makeCall(number: String) =
    try {
        startActivity(
            Intent(
                Intent.ACTION_DIAL,
                Uri.parse("tel:$number")
            ).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
        )
        true
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }

/**
 * 跳转到应用市场详情页面
 *
 * @param packageName 应用包名
 * @param marketPackage 应用市场包名{ @link ToolsExt.kt }, 为空时系统弹出选择列表
 */
fun Context.goToMarket(packageName: String, marketPackage: String = "") =
    try {
        startActivity(
            Intent(
                Intent.ACTION_VIEW,
                Uri.parse("market://details?id=$packageName")
            ).apply {
                if (marketPackage.isNotEmpty()) {
                    `package` = marketPackage
                }
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
            }
        )
        true
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }

/**
 * 防抖动点击事件，默认时间1s，默认单位秒
 */
@SuppressLint("CheckResult")
inline fun <reified T : View> T.oneClick(
    duration: Long = 1,
    unit: TimeUnit = TimeUnit.SECONDS,
    crossinline listener: (View) -> Unit
) {
    clicks()
        .throttleFirst(duration, unit)
        .subscribe { listener.invoke(this) }
}

fun TabLayout.onTabSelectedListener(init: __TabLayout_OnTabSelectedListener.() -> Unit) {
    val listener = __TabLayout_OnTabSelectedListener()
    listener.init()
    addOnTabSelectedListener(listener)
}

/**
 * Iterate the receiver [List] using an index.
 */
inline fun <T> List<T>.forEachWithIndex(function: (Int, T) -> Unit) {
    for (index in 0 until size) function(index, get(index))
}

